import 'dart:io';
import 'package:example/pages/pages/HomePage.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_navigation/src/root/get_material_app.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

void main() {
  // 启动前运行代码设置
  WidgetsFlutterBinding.ensureInitialized();
  realRunApp();
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。
    // 写在组件渲染之后，是为了在渲染后进行set赋值,覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
      ///底部状态栏
      systemNavigationBarColor: Colors.transparent,

      ///顶部状态栏
      statusBarColor: Colors.transparent,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

realRunApp() async {
  // 启动加载数据
  // await SPUtils.getInstance();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return setRefreshConfiguration();
      },
    );
  }

  setRefreshConfiguration() {
    return RefreshConfiguration(
      headerBuilder: () => const ClassicHeader(
        idleText: "下拉刷新",
        releaseText: "松手刷新数据",
        refreshingText: "刷新中",
        completeText: "刷新完成",
        failedText: "刷新失败,请重试",
        refreshingIcon: SizedBox(
          width: 25.0,
          height: 25.0,
          child: CupertinoActivityIndicator(),
        ),
        refreshStyle: RefreshStyle.Follow,
      ),
      footerBuilder: () => const ClassicFooter(
        loadingText: "刷新中",
        noDataText: "无更多数据",
        idleText: "上拉刷新",
        failedText: "刷新失败,请重试",
        canLoadingText: "松手刷新数据",
        //底部指示器只有刷新的时候显示
        loadStyle: LoadStyle.ShowWhenLoading,
        loadingIcon: SizedBox(
          width: 25.0,
          height: 25.0,
          child: CupertinoActivityIndicator(),
        ),
      ),
      //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
      enableScrollWhenRefreshCompleted: true,
      // 不满一屏时禁止上拉
      hideFooterWhenNotFull: true,
      // 最大上拉显示距离
      maxUnderScrollExtent: 20,
      child: setGetMaterialApp(),
    );
  }

  setGetMaterialApp() {
    return GetMaterialApp(
      theme: ThemeData(
        // 取消tabbar点击时的高亮个水波纹
        tabBarTheme: TabBarTheme(
          overlayColor: WidgetStatePropertyAll(Colors.transparent),
        ),
        datePickerTheme: DatePickerThemeData(
          backgroundColor: Colors.white,
        ),
        appBarTheme: const AppBarTheme(
          iconTheme: IconThemeData(
            color: Color(0xE6000000),
          ),
          centerTitle: true,
          titleTextStyle: TextStyle(
            color: Color(0xff333333),
            fontWeight: FontWeight.bold,
            fontSize: 18,
          ),
          backgroundColor: Colors.white,
          elevation: 0,
          scrolledUnderElevation: 0,
        ),
        scaffoldBackgroundColor: Colors.white,
        // 点击时点击效果
        splashColor: Colors.transparent,
        // 长按时点击效果
        highlightColor: Colors.transparent,
        // 主题颜色
        colorScheme: ColorScheme.fromSeed(
          seedColor: Colors.black,
          primary: Colors.black,
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ButtonStyle(
              backgroundColor: WidgetStatePropertyAll(Color(0xFF3F83F6)),
              foregroundColor: WidgetStatePropertyAll(Colors.white)),
        ),
      ),
      title: "巨能管Plus",
      //当前运行环境配置
      locale: const Locale("zh", "CH"),
      //程序支持的语言环境配置
      supportedLocales: const [Locale("zh", "CH"), Locale('en', 'US')],
      //Material 风格代理配置
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      debugShowCheckedModeBanner: false,
      home: HomePage(),
      builder: EasyLoading.init(
        builder: (context, widget) {
          return MediaQuery(
            ///设置文字大小不随系统设置改变
            data: MediaQuery.of(context).copyWith(
              textScaler: const TextScaler.linear(1),
            ),
            child: widget ?? Container(),
          );
        },
      ),
    );
  }
}
